package com.filemanager;

import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeNode;
import java.io.File;

public class FileUtils {
    public static void createChildren(File fileRoot, DefaultMutableTreeNode node) {
        File[] files = fileRoot.listFiles();
        if (files == null) return;
        for (File file : files) {
            DefaultMutableTreeNode childNode = new DefaultMutableTreeNode(file.getName());
            node.add(childNode);
            if (file.isDirectory()) {
                createChildren(file, childNode);
            }
        }
    }
    public static File getFileFromNode(DefaultMutableTreeNode node, File rootFile) {
        TreeNode[] path = node.getPath();
        StringBuilder fullPath = new StringBuilder(rootFile.getAbsolutePath());
        for (int i = 1; i < path.length; i++) {
            fullPath.append(File.separator).append(path[i].toString());
        }
        return new File(fullPath.toString());
    }
    public static File getFileFromNode(DefaultMutableTreeNode node) {
        return getFileFromNode(node, new File(System.getProperty("user.dir")));
    }
} 